<?php
    use User\Model\MeetingModel;
?>
<!-- page heading start-->
<div class="page-heading">
    <h3>
        报名表单
    </h3>

</div>
<!-- page heading end-->
<div class="row">
    <div class="col-sm-12">
        <section class="panel">
            <header class="panel-heading">
                常用字段：
                <div class="btn btn-default" onclick="usage_click('姓名', '<?php echo MeetingModel::FIELD_TEXT?>', '<?php echo MeetingModel::FORMAT_NONE?>');">姓名</div>
                <div class="btn btn-default" onclick="usage_click('公司', '<?php echo MeetingModel::FIELD_TEXT?>', '<?php echo MeetingModel::FORMAT_NONE?>');">公司</div>
                <div class="btn btn-default" onclick="usage_click('职位', '<?php echo MeetingModel::FIELD_TEXT?>', '<?php echo MeetingModel::FORMAT_NONE?>');">职位</div>
                <div class="btn btn-default" onclick="usage_click('手机', '<?php echo MeetingModel::FIELD_TEXT?>', '<?php echo MeetingModel::FORMAT_PHONE?>');">手机</div>
                <div class="btn btn-default" onclick="usage_click('地址', '<?php echo MeetingModel::FIELD_ADDRESS?>', '<?php echo MeetingModel::FORMAT_NONE?>');">地址</div>
                <div class="btn btn-default" onclick="usage_click('邮箱', '<?php echo MeetingModel::FIELD_TEXT?>', '<?php echo MeetingModel::FORMAT_EMAIL?>');">邮箱</div>
                <div class="btn btn-default" onclick="usage_click('到达离开时间', '<?php echo MeetingModel::FIELD_RANGE?>', '<?php echo MeetingModel::FORMAT_NONE?>');">到达离开时间</div>
                <div class="btn btn-default" onclick="usage_click('身份证', '<?php echo MeetingModel::FIELD_TEXT?>', '<?php echo MeetingModel::FORMAT_ID?>');">身份证</div>
                <div class="btn btn-default" onclick="usage_click('城市', '<?php echo MeetingModel::FIELD_CITY?>', '<?php echo MeetingModel::FORMAT_NONE?>');">城市</div>
            </header>
            <div class="panel-body">
                <button class="btn btn-success" data-toggle="modal" data-target="#myModal">新建</button>
                <table class="table table-hover general-table table-bordered">
                    <thead>
                        <tr>
                            <th style="width:15%;">字段名称</th>
                            <th style="width:15%;">字段类型</th>
                            <th style="width:15%;">字段格式</th>
                            <th style="width:10%;">是否必填</th>
                            <th style="width:10%;">是否做为查询条件</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php foreach($list as $val){?>
                    <tr data-field-id="<?php echo $val['field_id'];?>">
                        <td>
                            <span>
                                <?php echo $val['name'];?>
                            </span>
                        </td>
                        <td>
                            <span>
                                <?php echo MeetingModel::getFieldType($val['type']);?>
                            </span>
                        </td>
                        <td>
                            <span>
                                <?php echo MeetingModel::getFieldFormat($val['format']);?>
                            </span>
                        </td>
                        <td>
                            <span>
                                <?php echo empty($val['is_require']) ? "否" : "是";?>
                            </span>
                        </td>
                        <td>
                            <span>
                                <?php echo empty($val['is_condition']) ? "否" : "是";?>
                            </span>
                        </td>
                        <td>
                            <button class="btn btn-success" onclick="edit_field(this);">编辑</button>
                            <a class="btn btn-default" href="#"
                                   onclick="<?php echo 'return delete_data(' . $val['field_id'] .')';?>">删除</a>
                            <img src='/Public/images/arrow_up.png' style='margin-left:30px;width: 25px;height: 50px;' onclick='change_up(this)'>
                            <img src='/Public/images/arrow_down.png' style='margin-left:20px;width: 25px;height: 50px;;' onclick='change_down(this)'>
                        </td>
                    </tr>
                    <?php }?>
                    </tbody>
                </table>
            </div>
            <div class="panel-body" style="width: 50%">
                <div class="form-group">
                    <label class="control-label">报名时间</label>
                    <br>
                    <input type="text" class="form-control" id="date-picker1" style="width: 40%;float: left" value="<?php $val=(!empty($meet) && ($meet['register_start_date']!= '0000-00-00 00:00:00')) ?
                            $meet['register_start_date'] : ''; echo $val;?>" placeholder="请输入报名开始时间" ><span style="float: left">-</span>
                    <input type="text" class="form-control" id="date-picker2" style="width: 40%"  value="<?php $val=(!empty($meet) && ($meet['register_end_date']!= '0000-00-00 00:00:00')) ?
                            $meet['register_end_date'] : ''; echo $val;?>" placeholder="请输入报名结束时间" >
                </div>
                <div class="form-group">
                    <label class="control-label">是否需要电子票</label>
                    <div>
                    <input type="radio" name="ticket" value="<?php echo MeetingModel::TICKET_NO?>"
                           <?php if(!$meet['need_ticket']){ echo 'checked';}?>>否
                    <input type="radio" name="ticket" value="<?php echo MeetingModel::TICKET_YES;?>"
                           <?php if($meet['need_ticket']){ echo 'checked';}?>>是
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">电子票描述</label>
                    <textarea class="form-control" id="ticket_desc">{$meet['ticket_desc']}</textarea>
                </div>
                <div class="form-group">
                    <label class="control-label">报名成功后</label>
                    <div>
                        <input type="radio" name="result_type" value="<?php echo MeetingModel::RESULT_LINK?>"
                           <?php if(!$meet['result_type']){ echo 'checked';}?>>弹窗
                        <input type="radio" name="result_type" value="<?php echo MeetingModel::RESULT_URL;?>"
                           <?php if($meet['result_type']){ echo 'checked';}?>>跳转外链
                        <input type="text" value="{$meet['result_msg']}" class="form-control" name="result_msg" placeholder="外链地址/弹框内容">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">是否需要审核</label>
                    <div>
                    <input type="radio" name="verify" value="<?php echo MeetingModel::VERIFY_NO?>"
                           <?php if(!$meet['need_verify']){ echo 'checked';}?>>否
                    <input type="radio" name="verify" value="<?php echo MeetingModel::VERIFY_YES;?>"
                           <?php if($meet['need_verify']){ echo 'checked';}?>>是
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">是否提供乘车服务</label>
                    <div>
                        <input type="radio" name="bus_service" value="<?php echo MeetingModel::BUS_NO?>"
                        <?php if(!$meet['bus_service']){ echo 'checked';}?>>否
                        <input type="radio" name="bus_service" value="<?php echo MeetingModel::BUS_YES;?>"
                        <?php if($meet['bus_service']){ echo 'checked';}?>>是
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">是否提供餐饮服务</label>
                    <div>
                        <input type="radio" name="eat_service" value="<?php echo MeetingModel::EAT_NO?>"
                        <?php if(!$meet['eat_service']){ echo 'checked';}?>>否
                        <input type="radio" name="eat_service" value="<?php echo MeetingModel::EAT_YES;?>"
                        <?php if($meet['eat_service']){ echo 'checked';}?>>是
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">是否提供住宿服务</label>
                    <div>
                        <input type="radio" name="hotel_service" value="<?php echo MeetingModel::HOTEL_NO?>"
                        <?php if(!$meet['hotel_service']){ echo 'checked';}?>>否
                        <input type="radio" name="hotel_service" value="<?php echo MeetingModel::HOTEL_YES;?>"
                        <?php if($meet['hotel_service']){ echo 'checked';}?>>是
                    </div>
                </div>
                <div class="form-group" style="display: none">
                    <label class="control-label">报名人数上限</label>
                    <input type="text" class="form-control" value="{$meet['register_limit']}" id="register_limit">
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" onclick="update_menu()">提交</button>
                </div>
            </div>
        </section>
    </div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-keyboard="false">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                <input type="hidden" value="" id="field_id">
                <div>标题<input type='text' class="form-control" id="name"></div>
                <div style="width: 200px;">类型<select class="form-control" id="type" name="type">
                    <?php foreach(MeetingModel::getFieldType() as $k => $v){
                        echo "<option value='$k'>$v</option>";
                    }?>
                </select></div>
                <div style="width: 200px;" class="modal-format">格式<select class="form-control" id="format" name="format">
                    <?php foreach(MeetingModel::getFieldFormat() as $k => $v){
                    echo "<option value='$k'>$v</option>";
                    }?>
                </select></div>
                <div style="display: none" class="modal-option">选项
                    <div class="option" style="margin-top: 10px;"><input type="text" class="form-control" style="width: 60%;float: left"><button class="btn" style="margin-left: 10px;" onclick="remove_option(this);">-</button></div>
                    <button class="btn add_option" style="margin-top: 10px;" onclick="add_option(this);">+</button>
                </div>
                <div style="display: none" class="modal-range">
                    范围<br>
                    <input type="text" id="min_value" class="form-control" style="float:left;width: 20%" placeholder="最小值"><span style="float: left;font-size: larger">-</span><input type="text" class="form-control" style="width: 20%;" placeholder="最大值" id="max_value" >
                </div>
                <div style="margin-top: 10px;" class="modal-require">
                    <span >是否需要必填</span>
                    <input type="radio" name="require" value="<?php echo MeetingModel::REQUIRE_NO?>"
                           onclick="set_require(this);" >否
                    <input type="radio" name="require" value="<?php echo MeetingModel::REQUIRE_YES;?>"
                           onclick="set_require(this);">是
                </div>
                <div style="margin-top: 10px;" class="modal-search">
                    <span >做为查询条件</span>
                    <input type="radio" name="condition" value="<?php echo MeetingModel::CONDITION_NO?>"
                           onclick="set_condition(this);" >否
                    <input type="radio" name="condition" value="<?php echo MeetingModel::CONDITION_YES?>"
                           onclick="set_condition(this);">是
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="update_field()">提交更改</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<script>
    function delete_data(id){
        $.ajax({
            data:{fieldId:id},
            dataType:'json',
            url:'/User/Meeting/fieldDelete',
            type:'get',
            success:function(res){
                window.location.reload();
            }
        });
        return false;
    }
    function add_option(addoption){
        var option = '<div class="option" style="margin-top: 10px;"><input type="text" class="form-control" style="width: 60%;float: left"><button class="btn" style="margin-left: 10px;" onclick="remove_option(this);">-</button></div>';
        $(addoption).before(option);
    }
    function remove_option(option){
        $(option).closest("div").remove();
    }
    var is_require = 0;
    function set_require(require){
        is_require = $(require).val();
    }
    var is_condition = 0;
    function set_condition(condition){
        is_condition = $(condition).val();
    }
    var meetId = <?php echo I('get.meetId', -1, '');?>;
    var submit_option = new Array();
    function update_field(){
        var submit_option = new Array();
        $.each($('.option'), function(i){
            if($(this).children(".form-control").val()){
                submit_option[i] = $(this).children(".form-control").val();
            }
        });
        $.ajax({
            type:'get',
            dataType: 'json',
            url: '/User/Meeting/fieldUpdate',
            data:{
                fieldId: $("#field_id").val(),
                meetId: meetId,
                name: $("#name").val(),
                type: $("#type").val(),
                format: $("#format").val(),
                is_require: is_require,
                is_condition: is_condition,
                option: submit_option,
                min_value: $("#min_value").val(),
                max_value: $("#max_value").val()
            },
            success:function(res){
                alert(res.msg);
                window.location.reload();
            }
        });
    }
    function update_menu(){
        $.ajax({
            type:'post',
            dataType: 'json',
            url: '/User/Meeting/menuFieldUpdate',
            data:{
                meetId: meetId,
                need_ticket: $("input[name='ticket']:checked").val(),
                ticket_desc: $("#ticket_desc").val(),
                need_verify: $("input[name='verify']:checked").val(),
                eat_service: $("input[name='eat_service']:checked").val(),
                hotel_service: $("input[name='hotel_service']:checked").val(),
                bus_service: $("input[name='bus_service']:checked").val(),
                result_type: $("input[name='result_type']:checked").val(),
                result_msg: $("input[name='result_msg']").val(),
                register_start_date: $("#date-picker1").val(),
                register_end_date: $("#date-picker2").val(),
                register_limit: $("#register_limit").val()
            },
            success:function(res){
                window.location.reload();
            }
        });
    }
    $("#myModal").on('hidden.bs.modal', function(){
        $("#name").val('');
        var type = "option[value='0']";
         $("select[name='type']").find(type).prop('selected','true');
        var format = "option[value='0']";
         $("select[name='format']").find(format).prop('selected','true');
        is_require = 0;
        $("input[name='require']:checked").removeAttr('checked');
        var require = "input[name='require']:radio[value='0']";
         $(require).prop('checked', 'true');
        is_condition = 0;
        $("input[name='condition']:checked").removeAttr('checked');
        var require = "input[name='condition']:radio[value='0']";
         $(require).prop('checked', 'true');
        option = new Array();
        $.each($('.option'),function (i) {
            $(this).remove();
        });
        var addoption = '<div class="option" style="margin-top: 10px;"><input type="text" class="form-control" style="width: 60%;float: left"><button class="btn" style="margin-left: 10px;" onclick="remove_option(this);">-</button></div>';
         $(".add_option").before(addoption);

         $("#min_value").val('');
         $("#max_value").val('');
        $(".modal-format").css("display", "block");
        $(".modal-require").css("display", "block");
        $(".modal-search").css("display", "block");
        $(".modal-option").css("display", "none");
    });
    function change_down(field){
        var $field_tr_ori = $(field).closest("tr");
        var $field_tr_des = $field_tr_ori.next("tr");
        if($field_tr_des){
            $field_tr_ori.insertAfter($field_tr_des);
            $.ajax({
                type: 'get',
                dataType:'json',
                url:'/User/Meeting/fieldPriorityInc',
                data:{
                    fieldId:$field_tr_ori.attr('data-field-id'),
                    meetId:meetId
                },
                success:function(res){

                }
            });
        }
        return false;
    }

    function change_up(field){
        var $field_tr_ori = $(field).closest("tr");
        var $field_tr_des = $field_tr_ori.prev("tr");
        if($field_tr_des){
            $field_tr_des.insertAfter($field_tr_ori);
            $.ajax({
                type: 'get',
                dataType:'json',
                url:'/User/Meeting/fieldPriorityDec',
                data:{
                    fieldId:$field_tr_ori.attr('data-field-id'),
                    meetId:meetId
                },
                success:function(res){

                }
            });
        }
        return false;
    }
    function edit_field(field){
        var fieldId = $(field).closest("tr").attr('data-field-id');
        $.ajax({
            type: 'get',
            dataType: 'json',
            data:{
                fieldId: fieldId
            },
            url: '/User/Meeting/getField',
            success:function(result){
                var res = result.msg;
                if(res){
                    $("#field_id").val(res['field_id']);
                    $("#name").val(res['name']);
                    var type = "option[value='" + res['type'] + "']";
                    $("select[name='type']").find(type).prop('selected','true');
                    var format = "option[value='" + res['format'] + "']";
                    $("select[name='format']").find(format).prop('selected','true');
                    is_require = res['is_require'];
                    var require = "input[name='require']:radio[value=" + res['is_require'] + "]";
                    $(require).prop('checked', 'true');
                    is_condition = res['is_condition'];
                    var condition = "input[name='condition']:radio[value=" + res['is_condition'] + "]";
                    $(condition).prop('checked', 'true');
                    if(res['option']){
                        $.each($('.option'),function (i) {
                            $(this).remove();
                        });
                        submit_option = eval('(' + res['option'] + ')');
                        var addoption = '';
                        for(var i=0; i < submit_option.length; i++){
                            addoption = '<div class="option" style="margin-top: 10px;"><input type="text" value="'+ submit_option[i] + '" class="form-control" style="width: 60%;float: left"><button class="btn" style="margin-left: 10px;" onclick="remove_option(this);">-</button></div>';
                            $(".add_option").before(addoption);
                        }
                    }
                    $("#min_value").val(res['min_value']);
                    $("#max_value").val(res['max_value']);
                    if(res['type'] == 0){
                        $(".modal-format").css("display", "block");
                        $(".modal-require").css("display", "block");
                        $(".modal-search").css("display", "block");
                    }else{
                        $(".modal-format").css("display", "none");
                        $(".modal-require").css("display", "none");
                        $(".modal-search").css("display", "none");
                    }
                    if(res['type'] == 2 || res['type'] == 3){
                        $(".modal-option").css("display", "block");
                    }else{
                        $(".modal-option").css("display", "none");
                    }
                }
                $("#myModal").modal("show");
            }
        });
    }


    //--常用字段--
    function usage_click(text, type, format){
        $.ajax({
            type:'get',
            dataType: 'json',
            url: '/User/Meeting/fieldUpdate',
            data:{
                fieldId: 0,
                meetId: meetId,
                name: text,
                type: type,
                format: format,
                is_require: 0,
                is_condition: 0,
                option: '',
                min_value: 0,
                max_value: 0
            },
            success:function(res){
                alert(res.msg);
                window.location.reload();
            }
        });
    }
    $("#type").change(function(){
        if($(this).val() == 0){
            $(".modal-format").css("display", "block");
            $(".modal-require").css("display", "block");
            $(".modal-search").css("display", "block");
        }else{
            $(".modal-format").css("display", "none");
            $(".modal-require").css("display", "none");
            $(".modal-search").css("display", "none");
        }
        if($(this).val() == 2 || $(this).val() == 3){
            $(".modal-option").css("display", "block");
        }else{
            $(".modal-option").css("display", "none");
        }
    });
</script>
<script type="text/javascript" src="/Public/js/laydatenew/laydate.js"></script>
<script type="text/javascript">
    laydate.render({
       elem: '#date-picker1',
        type: 'datetime'
    });
    laydate.render({
        elem: '#date-picker2',
        type: 'datetime'
    });
</script>